﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Willson.JsbManager.Comm;
using Willson.JsbManager.Bll;

namespace Willson.JsbManager.UI
{
    public partial class XTCompanyJhManagerForm : Willson.JsbManager.UI.BaseForm
    {
        private enum OperatingType
        {
            JdJh,
            Sum,
            Cjh
        }

        private List<JdJhError> ErrorInfo = null; 

        private OperatingType type;


        public XTCompanyJhManagerForm()
        {
            InitializeComponent();

            ErrorInfo = new List<JdJhError>();

            ImageList imglist = new ImageList();
            imglist.ImageSize = new Size(1, 20);
            listView.SmallImageList = imglist;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndWarning("真的要继续吗？") == DialogResult.No)
                return;
            
            if (listView.Items.Count > 0)
            {
                if (MessageUtil.ShowYesNoAndWarning("存在未成功处理的单位，真的要继续吗？") == DialogResult.No)
                    return;
            }


            SelectYearJdForm form = new SelectYearJdForm(this);
            form.ShowDialog();

            type = OperatingType.JdJh;
             
        }

        /// <summary>
        /// 通用选择年月信息函数
        /// </summary>
        /// <param name="year"></param>
        /// <param name="jd"></param>
        public override void SelectYearJD(int year, int jd)
        {
            if (year == 0 || jd == 0) return;
            
            if (type == OperatingType.JdJh)
            {
                ErrorInfo.Clear();
                
                BLLFactory<BLLJhManager>.Instance.CreateJdJh(year, jd, ref ErrorInfo);

                IEnumerator<JdJhError> ator = ErrorInfo.GetEnumerator();

                foreach(ListViewItem item in listView.Items)
                {
                    listView.Items.Remove(item);
                }

                while (ator.MoveNext())
                {
                    listView.Items.Add(ator.Current.CompanyName).SubItems.Add(ator.Current.CompanyAddress);
                }

                MessageUtil.ShowTips("季度计划生成完成!");
            }

            if (type == OperatingType.Sum)
            {
                BLLFactory<BLLJhManager>.Instance.CreateJdSum(year, jd);

                MessageUtil.ShowTips("季度用水汇总完成!");
            }

            if (type == OperatingType.Cjh)
            {
                BLLFactory<BLLJhManager>.Instance.CreateCjh(year, jd);

                MessageUtil.ShowTips("超计划量生成完成!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndWarning("真的要继续吗？") == DialogResult.No)
                return;

            if (listView.Items.Count > 0)
            {
                if (MessageUtil.ShowYesNoAndWarning("存在未成功处理的单位，真的要继续吗？") == DialogResult.No)
                    return;
            }


            type = OperatingType.Sum;

            SelectYearJdForm form = new SelectYearJdForm(this);
            form.ShowDialog();

            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndWarning("真的要继续吗？") == DialogResult.No)
                return;

            if (listView.Items.Count > 0)
            {
                if (MessageUtil.ShowYesNoAndWarning("存在未成功处理的单位，真的要继续吗？") == DialogResult.No)
                    return;
            }


            type = OperatingType.Cjh;

            SelectYearJdForm form = new SelectYearJdForm(this);
            form.ShowDialog();

            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (ErrorInfo.Count > 0)
            {
                if (MessageUtil.ShowYesNoAndWarning("存在未成功处理的单位，真的要关闭吗？") == DialogResult.No)
                    return;
            }
            Close();
        }
       
    }
}
